import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class Solution {
    static class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return null;
        }
        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        list.add(root.val);
        if(root.left!=null){
            preorderTraversal(root.left);
        }else{
            TreeNode tmp=stack.pop();
            preorderTraversal(tmp.right);
        }
        

       return list;



    }
}